package com.ayf.payment.game.api.controller;

import com.ayf.payment.game.api.dto.MerchantDTO;
import com.ayf.payment.game.api.dto.platform.MerchantSettleDayDTO;
import com.ayf.payment.game.api.dto.platform.SettleConfigDTO;
import com.ayf.payment.game.api.entity.Merchant;
import com.ayf.payment.game.api.exception.ParameterException;
import com.ayf.payment.game.api.service.MerchantService;
import com.ayf.payment.game.api.service.platform.MerchantCashOutService;
import com.ayf.payment.game.api.service.platform.SettleConfigService;
import com.ayf.payment.game.api.type.CommonConstants;
import com.ayf.payment.game.api.utils.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

/**
 * 商户提现接口
 *
 * @author wwl
 * @date 2020/3/12 11:26
 */
@RestController
@RequestMapping("/v/settle")
public class MerchantCashOutGameController {

    @Autowired
    private MerchantCashOutService merchantCashOutService;

    @Autowired
    private SettleConfigService settleConfigService;

    @Autowired
    private MerchantService merchantService;

    /**
     * 商户提现获取商户信息
     * @param merchantDTO
     * @return
     */
    @PostMapping("/getMerchantAccount")
    public CommonResult<MerchantSettleDayDTO> getMerchantAccount(MerchantDTO merchantDTO) throws Exception {
        MerchantSettleDayDTO db=merchantCashOutService.getMerchantSettleData(merchantDTO.getId());
        if (db == null){
            return new CommonResult(CommonConstants.RETURN_ERROR, "请完善结算银行信息",null);
        }
        SettleConfigDTO settleConfigDTO= settleConfigService.getSettleConfig();
        db.setMinWithdrawalAmount(settleConfigDTO.getMinWithdrawalAmount());
        db.setMaxWithdrawalAmount(settleConfigDTO.getMaxWithdrawalAmount());
        db.setMaxPayAmount(settleConfigDTO.getMaxPayAmount());
        db.setMinPayAmount(settleConfigDTO.getMinPayAmount());
        db.setAccountName(SensitiveDataUtil.accountNameDesensitization(db.getAccountName()));
        if(db.getSettleType()==1) {  //0银行卡 1支付宝
            db.setCardNo(SensitiveDataUtil.alipayAccountNoDesensitization(db.getCardNo()));
        }else{
            db.setCardNo(SensitiveDataUtil.cardNoDesensitization(db.getCardNo()));
        }
        return new CommonResult(CommonConstants.RETURN_SUCCESS, "成功", db);

    }

    /**
     * 商户提现
     * @param merchantSettleDayDTO
     * @return
     */
    @PostMapping("/merchantCashOut")
    public CommonResult merchantCashOut(MerchantDTO merchantDTO, @RequestBody MerchantSettleDayDTO merchantSettleDayDTO) throws Exception {
        if(merchantSettleDayDTO==null ){
            throw new ParameterException("参数不能为空");
        }
        if (StringUtils.isBlank(merchantSettleDayDTO.getPayPassword())) {
            throw new ParameterException("支付密码不能为空");
        }
        Merchant merchant = merchantService.find(merchantDTO.getId());
        if (StringUtils.isBlank(merchant.getPayPassword())) {
            throw new ParameterException("请先设置支付密码");
        }
        if (!MD5Util.sign(merchantSettleDayDTO.getPayPassword(), merchant.getPaySalt()).equals(merchant.getPayPassword())) {
            throw new ParameterException("支付密码输入错误");
        }
        SettleConfigDTO settleConfigDTO= settleConfigService.getSettleConfig();

        if(merchantSettleDayDTO.getSellteAmount().compareTo(settleConfigDTO.getMinWithdrawalAmount())==-1){
            throw new ParameterException("提现失败，提现最小金额："+settleConfigDTO.getMinWithdrawalAmount()+"元");
        }
        if(merchantSettleDayDTO.getSellteAmount().compareTo(settleConfigDTO.getMaxWithdrawalAmount())>0){
            throw new ParameterException("提现失败，提现最大金额："+settleConfigDTO.getMaxWithdrawalAmount()+"元");
        }
        merchantSettleDayDTO.setMerchantId(merchantDTO.getId());
        BigDecimal fee = MerchantSettleServiceChargeUtil.getServiceCharge(merchantSettleDayDTO.getMerchantId(),merchantSettleDayDTO.getSellteAmount(), settleConfigDTO.getFeeCustom());
        if (merchantSettleDayDTO.getSellteAmount().compareTo(fee)<=0){
            throw new ParameterException("提现失败，申请金额必须大于手续费"+fee+"元");
        }
        merchantSettleDayDTO.setSellteFee(fee);
        String traceId = SnowFlakeUtils.getTraceId();
        return merchantCashOutService.merchantSettleManual(merchantDTO.getLoginAccount(), merchantSettleDayDTO,traceId);

    }

    /**
     * 实时结算申请
     * @param merchantDTO
     * @param merchantSettleDayDTO
     * @return
     * @throws Exception
     */
    @PostMapping("/realTimeWithdrawalApply")
    public CommonResult realTimeWithdrawalApply(MerchantDTO merchantDTO, @RequestBody MerchantSettleDayDTO merchantSettleDayDTO) {
        if(merchantSettleDayDTO==null ){
            throw new ParameterException("参数不能为空");
        }
        if (StringUtils.isBlank(merchantSettleDayDTO.getPayPassword())) {
            throw new ParameterException("支付密码不能为空");
        }
        Merchant merchant = merchantService.find(merchantDTO.getId());
        if (StringUtils.isBlank(merchant.getPayPassword())) {
            throw new ParameterException("请先设置支付密码");
        }
        if (!MD5Util.sign(merchantSettleDayDTO.getPayPassword(), merchant.getPaySalt()).equals(merchant.getPayPassword())) {
            throw new ParameterException("支付密码输入错误");
        }
        SettleConfigDTO settleConfigDTO= settleConfigService.getSettleConfig();

        if(merchantSettleDayDTO.getSellteAmount().compareTo(settleConfigDTO.getMinWithdrawalAmount())==-1){
            throw new ParameterException("实时结算申请失败，最小金额："+settleConfigDTO.getMinWithdrawalAmount()+"元");
        }
        if(merchantSettleDayDTO.getSellteAmount().compareTo(settleConfigDTO.getMaxWithdrawalAmount())>0){
            throw new ParameterException("实时结算申请失败，最大金额："+settleConfigDTO.getMaxWithdrawalAmount()+"元");
        }
        merchantSettleDayDTO.setMerchantId(merchantDTO.getId());
        if (1==merchantSettleDayDTO.getCashOutType()){ // 结算到银行卡
            BigDecimal fee = MerchantSettleServiceChargeUtil.getServiceCharge(merchantSettleDayDTO.getMerchantId(),merchantSettleDayDTO.getSellteAmount(), settleConfigDTO.getFeeCustom());
            if (merchantSettleDayDTO.getSellteAmount().compareTo(fee)<=0){
                throw new ParameterException("实时结算申请失败，申请金额必须大于手续费"+fee+"元");
            }
            merchantSettleDayDTO.setSellteFee(fee);
        } else {
            merchantSettleDayDTO.setSellteFee(BigDecimal.ZERO);
        }
        String traceId = SnowFlakeUtils.getTraceId();
        return merchantCashOutService.realTimeWithdrawalApply(merchantDTO.getLoginAccount(), merchantSettleDayDTO,traceId);

    }
}
